Skip to content

feat(controller): allow custom labels on PolicyServer resources#1699

Open
flavio wants to merge 4 commits intokubewarden:mainfrom
flavio:policy-server-add-annotations-and-labels
Open

feat(controller): allow custom labels on PolicyServer resources#1699
flavio wants to merge 4 commits intokubewarden:mainfrom
flavio:policy-server-add-annotations-and-labels

Conversation

@flavio
Copy link
Copy Markdown
Member

@flavio flavio commented Apr 28, 2026

Add spec.labels and propagate spec.annotations to the Deployment ObjectMeta and Pod template. System labels always take precedence
over user-defined ones.

Fixes #651

I was cleaning up my notification when I found this GH issue. I just tossed my AI agent at that since it was an easy task.

Copilot AI review requested due to automatic review settings April 28, 2026 15:43
@flavio flavio requested a review from a team as a code owner April 28, 2026 15:43
@flavio flavio self-assigned this Apr 28, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds user-configurable metadata to PolicyServer-managed workloads, enabling custom discovery/selection via labels and supporting additional annotations while ensuring controller/system metadata wins on conflicts. This addresses the inability to persistently add labels/annotations to the PolicyServer Deployment/Pods (Fixes #651).

Changes:

  • Introduces spec.labels to PolicyServer and wires it into the generated Deployment metadata and Pod template labels with system-label precedence.
  • Propagates spec.annotations onto the Deployment ObjectMeta (and continues using it for Pod template annotations).
  • Updates CRD schemas and generated CRD docs to include the new labels field (plus DeepCopy updates).

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/controller/policyserver_controller_deployment.go Merges user spec.labels/spec.annotations into Deployment metadata and Pod template labels.
internal/controller/policyserver_controller_test.go Adds tests asserting custom label/annotation propagation and precedence behavior.
api/policies/v1/policyserver_types.go Adds PolicyServerSpec.Labels field with documentation.
api/policies/v1/zz_generated.deepcopy.go Updates deepcopy generation for the new Labels map.
config/crd/bases/policies.kubewarden.io_policyservers.yaml Extends CRD schema to include spec.labels.
charts/kubewarden-crds/templates/policies.kubewarden.io_policyservers.yaml Mirrors CRD schema update in Helm CRDs chart.
docs/crds/CRD-docs-for-docs-repo.md Documents new labels field in generated CRD docs (markdown).
docs/crds/CRD-docs-for-docs-repo.adoc Documents new labels field in generated CRD docs (asciidoc).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/controller/policyserver_controller_deployment.go
Comment thread internal/controller/policyserver_controller_deployment.go
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.04%. Comparing base (92bcf07) to head (06b17f2).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1699      +/-   ##
==========================================
- Coverage   82.11%   77.04%   -5.07%     
==========================================
  Files         129      172      +43     
  Lines       16961    21505    +4544     
==========================================
+ Hits        13927    16569    +2642     
- Misses       3034     4724    +1690     
- Partials        0      212     +212     
Flag Coverage Δ
go-tests 58.07% <100.00%> (?)
rust-tests 82.12% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Add spec.labels and propagate spec.annotations to the Deployment
ObjectMeta and Pod template. System labels always take precedence
over user-defined ones.

Fixes kubewarden#651

Signed-off-by: Flavio Castelli <fcastelli@suse.com>
Assisted-by: Claude Sonnet 4.6
@flavio flavio force-pushed the policy-server-add-annotations-and-labels branch from 9d14016 to 5d19003 Compare April 28, 2026 16:35
…n each reconcile

Mutating the existing maps in-place meant stale user-defined labels and
annotations were never removed when deleted from spec. Replaced with
fresh map construction so removals are reflected immediately.

Adds a regression test covering the removal scenario.

Signed-off-by: Flavio Castelli <fcastelli@suse.com>
Assisted-by: Claude Sonnet 4.6
Copy link
Copy Markdown
Member

@jvanz jvanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, LGTM. I just have a small suggestion

Comment thread internal/controller/policyserver_controller_deployment.go Outdated
Comment thread internal/controller/policyserver_controller_deployment.go
Take feedback from code review

Signed-off-by: Flavio Castelli <fcastelli@suse.com>
Comment thread internal/controller/policyserver_controller_deployment.go Outdated
Comment thread internal/controller/policyserver_controller_deployment.go
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/controller/policyserver_controller_deployment.go Outdated
…ss reconciles

Rebuilding the Deployment ObjectMeta annotations map from scratch on each
reconcile was deleting annotations added by Kubernetes controllers (e.g.
deployment.kubernetes.io/revision) and external tooling, causing reconcile
churn as those controllers re-added them.

Switch to a tracking-annotation approach: the controller records which
annotation/label keys it owns (kubewarden.io/managed-annotation-keys and
kubewarden.io/managed-label-keys) and only removes keys that were previously
managed but have since been deleted from spec. All other annotations/labels
set by Kubernetes or third-party tooling are left untouched.

Adds a regression test asserting that externally-added Deployment annotations
survive a reconcile that removes a user-defined annotation from spec.

Signed-off-by: Flavio Castelli <fcastelli@suse.com>
Assisted-by: Claude Sonnet 4.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Pending review

Development

Successfully merging this pull request may close these issues.

Feature Request: Ability to add custom labels/annotations to resources created by the policyserver

4 participants